package cn.kduck.code.entity.web;

import cn.kduck.code.entity.exception.ExistEntityCodeException;
import cn.kduck.code.entity.exception.ExistFieldCodeException;
import cn.kduck.code.entity.service.Entity;
import cn.kduck.code.entity.service.EntityField;
import cn.kduck.code.entity.service.EntityService;
import cn.kduck.core.web.json.JsonObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/code/entity")
public class EntityController {

    private final Log logger = LogFactory.getLog(getClass());

    @Autowired
    private EntityService entityService;

    @PostMapping("/add")
    public JsonObject addEntity(Entity entity){
        try {
            String id = entityService.addEntity(entity);
            return new JsonObject(id);
        } catch (ExistEntityCodeException e) {
            return new JsonObject(null,-1,"实体编码已经存在：" + e.getEntityCode());
        }
    }

    @PostMapping("/field/add")
    public JsonObject addEntityField(EntityField entityField){
        String id = entityService.addEntityField(entityField);
        return new JsonObject(id);
    }

    @DeleteMapping("/delete")
    public JsonObject deleteEntity(@RequestParam("ids") String[] ids){
        entityService.deleteEntity(ids);
        return JsonObject.SUCCESS;
    }

    @DeleteMapping("/field/delete")
    public JsonObject deleteEntityField(@RequestParam("entityId") String entityId,@RequestParam("ids") String[] ids){
        entityService.deleteEntityField(entityId,ids);
        return JsonObject.SUCCESS;
    }

    @PutMapping("/update")
    public JsonObject updateEntity(Entity entity){
        entityService.updateEntity(entity);
        return JsonObject.SUCCESS;
    }

    @PutMapping("/field/update")
    public JsonObject updateEntityField(EntityField entityField){
        entityService.updateEntityField(entityField);
        return JsonObject.SUCCESS;
    }


    @GetMapping("/get")
    public JsonObject getEntity(@RequestParam("entityId") String entityId){
        Entity entity = entityService.getEntity(entityId);
        return new JsonObject(entity);
    }

    @GetMapping("/field/get")
    public JsonObject getEntityField(@RequestParam("fieldId") String fieldId){
        EntityField entityField = entityService.getEntityField(fieldId);
        return new JsonObject(entityField);
    }

    @GetMapping("/list")
    public JsonObject listEntity(){
        List<Entity> entityList = entityService.listEntity();
        return new JsonObject(entityList);
    }

    @GetMapping("/field/list")
    public JsonObject listEntityField(@RequestParam("entityId") String entityId,@RequestParam(value = "fieldNameOrCode",required = false)String fieldNameOrCode){
        List<EntityField> fieldList = entityService.listEntityField(entityId,fieldNameOrCode);
        return new JsonObject(fieldList);
    }

    @PutMapping("/field/sort")
    public JsonObject sortEntityField(@RequestParam("sortedId") String[] sortedIds){
        entityService.sortEntityField(sortedIds);
        return JsonObject.SUCCESS;
    }

}
